<template>
  <Back />
  <ScanQr @result="handleScanResult" />
</template>

<script lang="ts" setup>
import { ScanQr } from 'vue-tg'

const router = useRouter()
const store = useAppStore()

async function handleScanResult(data: string) {
  // showToast(data)
  const tgUser = store.tgUser!
  let startParam = '0'
  if (data.indexOf('https://t.me/teawoo_bot/teawoo/?startapp=') == 0) {
    startParam = data.replace('https://t.me/teawoo_bot/teawoo/?startapp=', '')
  }

  const action = await apis.api.loginActionUser({ openid: tgUser.id, firstName: tgUser.first_name, action: startParam })
  if (action.code > 0 && action.msg && action.msg.length > 0) {
    showDialog({ message: action.msg })
  }

  router.replace('/home')
}
</script>
